///<reference path="../init.d.ts"/>
// 观察者模式对象
let observer = (function () {
    // 私有变量
    let _messages = {};
    return {
        regist(eventName, callback) {
            if (!_messages.hasOwnProperty(eventName)) {
                _messages[eventName] = [callback];
            }
            else {
                _messages[eventName].push(callback);
            }
        },
        fire(eventName, args = {}) {
            if (!_messages[eventName])
                return;
            let events = { eventName, args }, i = 0, len = _messages[eventName].length;
            for (; i < len; i++) {
                _messages[eventName][i].call(this, events);
            }
        },
        remove(eventName, callBack) {
            if (_messages[eventName] instanceof Array && _messages[eventName].length > 0) {
                for (let i = 0, len = _messages[eventName].length; i < len; i++) {
                    if (_messages[eventName][i] === callBack) {
                        _messages[eventName].splice(i, 1);
                        break;
                    }
                }
            }
        }
    };
})();
export default observer;
